/**
 * Created by Administrator on 2017/1/16 0016.
 */

var pageSize = 2;//每页条数
var pageStart = 1;//当前页码
var pageCount = 10;//总页数
var islogin = 1;

$(function () {
    $(".left-nav-item").eq(0).click(function () {
        window.location.href = "card_pc.html";
    });
    $(".left-nav-item").eq(1).click(function () {
        window.location.href = "apply.record_pc_student.html";
    });
    $(".apply-out").click(function () {
        window.location.href = "apply.detail_pc_student.html";
    });
    $(".exit-btn").click(function () {
        $.ajax({
            url: "/door/StudentFront/exit?" + new Date().getTime(),
            async: false,
            success: function (){
                window.location.href = "login_pc_student.html";
            }
        });
    });
    init();
});

function init() {
    $.ajax({
        url: "/door/StudentFront/getCurStu?" + new Date().getTime(),
        async: false,
        success: function (str) {
            if (checkReturn(str)) {
                $("#stu_name").text(str.name);
                $("#class_name").text(str.classname);
            }
        }
    });
    getOutRequest();
}

function getOutRequest() {
    $.ajax({
        url: "/door/StudentFront/getOutRequest?" + new Date().getTime(),
        type: "post",
        data: {
            pageSize: pageSize,
            pageStart: pageStart
        },
        async: false,
        success: function (str) {
            if (checkReturn(str)) {
                $(".right-detail>div").remove();
                for (var i = 0; i < str.length; i++) {
                    var status_str = null;
                    var status_class = null;
                    switch (str[i].authStatus){
                        case 0 :
                            status_str = "审批中";
                            status_class = "state-ing";
                            break;
                        case 1 :
                            status_str = "审批通过";
                            status_class = "state-yes";
                            break;
                        case 2 :
                            status_str = "审批驳回";
                            status_class = "state-no";
                            break;
                        case 3 :
                            status_str = "撤销";
                            status_class = "state-cancel";
                            break;
                    }
                    $(".right-detail").append(
                        "<div class='apply-item text-center'><div><p>"
                        +str[i].classname+"</p><p>"
                        +str[i].studentname+"</p></div><div><label>请假时长：</label><p>"
                        +DateUtils.format(new Date(str[i].startTime), 'yyyy-MM-dd HH:mm:ss')+"</p><p>至</p><p>"
                        +DateUtils.format(new Date(str[i].endTime), 'yyyy-MM-dd HH:mm:ss')+"</p><p class="
                        +status_class+">"
                        +status_str+"</p></div><div><label>请假原因：</label><div class='reason'>"
                        +str[i].requestReason+"</div><a href='#' onclick='hisonclick("+str[i].id+")'><p class='to-detail'>查看详情>></p></a></div></div>"
                    );
                }
            }
        }
    });
    $.ajax({
        url: "/door/StudentFront/getOutRequest?" + new Date().getTime(),
        type: "post",
        async: false,
        success: function (str) {
            if (str != 0) {
                pageCount = Math.floor((str + pageSize - 1) / pageSize);
                setTableBTN();
            } else {
                alert("没有数据");
            }
        }
    });
}

function hisonclick(id){
	sessionStorage.setItem("id", id);
	window.location.href="/door/front_end/pc/student/apply.detailsPC_student.html";//跳转到详情页...
}

function checkReturn(str) {
    if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    alert(str.errormessage);
                    window.location.href = "login_pc_student.html";
                    islogin = 0;
                }
            }
            if (str.errorcode == 0) {
                alert(str.errormessage);
                return true;
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

function setTableBTN() {
    var pagination_li = $(".pagination li");
    var pagination_a = $(".pagination a");

    pagination_li.each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
                if (pageStart > 1) {
                    pageStart--;

                    if (pageStart < parseInt(pagination_a.eq(2).html())) {
                        pagination_a.eq(6).html(pageStart + 4);
                        pagination_a.eq(5).html(pageStart + 3);
                        pagination_a.eq(4).html(pageStart + 2);
                        pagination_a.eq(3).html(pageStart + 1);
                        pagination_a.eq(2).html(pageStart);
                    }
                    pagination_li.each(function (i) {
                        $(this).removeClass("active");
                        if (pageStart == parseInt($(this).text())) {
                            $(this).addClass("active");
                        }
                    });
                    getOutRequest();
                } else {
                    layer.msg("当前是第一页", {time: 1000});
                }
            };
        } else if (i == 7) {//下一页按钮
            this.onclick = function () {
                if (pageStart < pageCount) {
                    pageStart++;

                    if (pageStart > parseInt(pagination_a.eq(6).html())) {
                        pagination_a.eq(6).html(pageStart);
                        pagination_a.eq(5).html(pageStart - 1);
                        pagination_a.eq(4).html(pageStart - 2);
                        pagination_a.eq(3).html(pageStart - 3);
                        pagination_a.eq(2).html(pageStart - 4);
                    }
                    pagination_li.each(function (i) {
                        $(this).removeClass("active");
                        if (pageStart == parseInt($(this).text())) {
                            $(this).addClass("active");
                        }
                    });
                    getOutRequest();
                } else {
                    layer.msg("当前是最后一页", {time: 1000});
                }
            };
        } else if (i == 0) {
            this.onclick = function () {
                pageStart = 1;

                if (pageStart < parseInt(pagination_a.eq(2).html())) {
                    pagination_a.eq(6).html(pageStart + 4);
                    pagination_a.eq(5).html(pageStart + 3);
                    pagination_a.eq(4).html(pageStart + 2);
                    pagination_a.eq(3).html(pageStart + 1);
                    pagination_a.eq(2).html(pageStart);
                }
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageStart == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                getOutRequest();
            };
        } else if (i == 8) {
            this.onclick = function () {
                pageStart = pageCount;

                if (pageStart > parseInt(pagination_a.eq(6).html())) {
                    pagination_a.eq(6).html(pageStart);
                    pagination_a.eq(5).html(pageStart - 1);
                    pagination_a.eq(4).html(pageStart - 2);
                    pagination_a.eq(3).html(pageStart - 3);
                    pagination_a.eq(2).html(pageStart - 4);
                }
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageStart == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                getOutRequest();
            };
        } else {//普通翻页按钮
            this.onclick = function () {
                var index = parseInt(this.innerText);
                if (index > pageCount) {
                    index = pageCount;
                }
                pageStart = index;
                pagination_li.each(function (i) {
                    $(this).removeClass("active");
                    if (pageStart == parseInt($(this).text())) {
                        $(this).addClass("active");
                    }
                });
                // pageStart = parseInt(this.);
                getOutRequest();
            }
        }
    });
}